package com.imooc.miaosha.controller.FeController;

import com.alibaba.fastjson.TypeReference;
import com.imooc.miaosha.common.Const;
import com.imooc.miaosha.dto.User.RegisterDTO;
import com.imooc.miaosha.dto.UserDTO;
import com.imooc.miaosha.pojo.User;
import com.imooc.miaosha.service.UserService;
import com.imooc.miaosha.util.*;

import com.imooc.miaosha.validator.ValidatorUtil;
import com.imooc.miaosha.vo.LoginVO;

import jdk.nashorn.internal.runtime.ConsString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
@Slf4j
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("to_login")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("do_regist")
    @ResponseBody
    public JsonData register(@RequestBody RegisterDTO registerDTO) {
        ValidatorUtil.validate(registerDTO);
        String salt = userService.userExistAndReturnSalt(registerDTO.getMobile());
        if (salt != null) {
            return JsonData.fail("用户已存在,无法进行注册,请登录");
        }
        log.info(registerDTO.toString());
        log.info(registerDTO.getNickName());
        int count = userService.registerUser(registerDTO);
        if (count == 0) {
            return JsonData.fail("注册失败😔");
        } else {
            return JsonData.success("注册成功🍄");
        }
    }

    @RequestMapping(value = "do_login")
    @ResponseBody
    public JsonData doLogin(@RequestBody LoginVO loginVO, HttpServletResponse response) {
        ValidatorUtil.validate(loginVO);
        String salt = userService.userExistAndReturnSalt(loginVO.getMobile());
        if (salt == null) {
            return JsonData.fail("用户不存在,请点击右上角开关进行注册😁");
        }
        UserDTO userDTO = userService.login(loginVO.getPassword(), loginVO.getMobile(), salt);
        if (userDTO == null) {
            return JsonData.fail("密码错误😔");
        }
        String token = UUIDUtil.UUID();
        CookieUtil.writeCookie(response, token);
        JedisUtil.setEx(Const.LOGIN_TOKEN_PREFIX, token, JsonUtil.Obj2String(userDTO), 60 * 30);
        return JsonData.success("登录成功😄", token);

    }

    @RequestMapping("get_user_info_mini")
    @ResponseBody
    public JsonData getUserInfo(String loginToken) {
        String userJson = JedisUtil.get(Const.LOGIN_TOKEN_PREFIX, loginToken);

        UserDTO userDTO = JsonUtil.String2Obj(userJson, new TypeReference<UserDTO>() {
        });
        if (userDTO == null) {
            return JsonData.fail("用户未登录？");
        }
        return JsonData.success(userDTO);

    }

    @RequestMapping("get_user_info")
    @ResponseBody
    public JsonData getUserInfo(HttpServletRequest request) {
        String loginToken = CookieUtil.readCookie(request);

        String userJson = JedisUtil.get(Const.LOGIN_TOKEN_PREFIX, loginToken);
        UserDTO userDTO = JsonUtil.String2Obj(userJson, new TypeReference<UserDTO>() {
        });
        if (userDTO == null) {
            return JsonData.fail("用户未登录？");
        }
        return JsonData.success(userDTO);

    }


}